package com.javafactroy.kunlong.bip.icp.message.data.base;

import com.javafactroy.kunlong.bip.icp.common.consts.IConfig;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import com.javafactroy.kunlong.bip.icp.message.base.base.Message;
import com.javafactroy.kunlong.bip.icp.message.base.iface.IMessage;
import com.javafactroy.kunlong.bip.icp.message.base.consts.AppEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class AppMsg extends Message implements IMessage {
    // single
    AppHeader appHeader;
    AppBody appBody;

    // pageinfo
    AppResult appResult;

    //工具类
    AppByteBuffer appByteBuffer;

    public AppMsg() {
        appHeader = new AppHeader();
        appBody = new AppBody();
        appResult = new AppResult();

        appByteBuffer = new AppByteBuffer();
        appByteBuffer.setAppType(AppEnum.AppTypeEnum.appdata);
        appByteBuffer.setMsgType(AppEnum.MsgTypeEnum.request);
    }

    public void init(byte[] bytes, AppEnum appEnum) {
        appByteBuffer.init(bytes, appEnum);
        //getAppPosition().setHeaderStart(0);
        //getAppPosition().setHeaderEnd(32);

    }

    public void append(byte[] bytes, AppEnum appEnum) {
        appByteBuffer.append(bytes,appEnum);

    }

    public AppByteBuffer.AppPosition getAppPosition() {
        return appByteBuffer.getAppPosition();
    }
    public void appBody2Result(){

    }

    void decode() {

        appByteBuffer.decode(this, AppEnum.header);
        appByteBuffer.decode(this, AppEnum.body);
        appByteBuffer.decode(this, AppEnum.page);
    }

    public byte[] encode() {
        appByteBuffer.encode(this, AppEnum.header);
        appByteBuffer.encode(this, AppEnum.body);
        appByteBuffer.encode(this, AppEnum.page);
        return new byte[1];

    }

    @Override
    public ByteBuf encode2ByteBuf() {
        return Unpooled.wrappedBuffer(encode());

    }

    @Override
    public void decode(byte[] buf) {

    }

    @Override
    public void check() {

    }

    @Override
    public IConfig.INettyConst.MSG_State getMsgState() {
        return null;
    }

    @Override
    public String getMessageId() {
        return ""+appHeader.getMsgId();
    }
    @Override
    public String getMsgType()
    {
        return getAppHeader().getMsgType();

    }

}
